package com.shushan.permission;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.shushan.base.NavigationLoseActivity;
import com.shushan.shushanbao.R;
import com.shushan.ui.CustomDialog;
import com.shushan.util.OpenSettingUtils;

/**
 * @ClassName: BackgroundPermissionActivity
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-12-29
 */
public class BackgroundPermissionActivity extends NavigationLoseActivity implements View.OnClickListener {

    private static final String TAG = "BackgroundPermissionActivity";

    private Button nextButton;
    private Button okButton;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("后台弹出界面");

        setContentView(R.layout.background_permission_activity);

        initView();
    }

    private void initView() {
        nextButton = findViewById(R.id.background_permission_next_btn);
        nextButton.setOnClickListener(this);
        okButton = findViewById(R.id.background_permission_ok_btn);
        okButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.background_permission_next_btn:
                setAllowBack(true);
                OpenSettingUtils.openVivoBackground(this);
                break;
            case R.id.background_permission_ok_btn:
                onBackAction(700);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                showBackDialog();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showBackDialog() {
        new CustomDialog.Builder(this)
                .setTitle("后台弹出界面权限")
                .setMessage("是否已开启后台弹出界面权限？")
                .setNegativeButton("已开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        onBackAction(700);
                    }
                })
                .setPositiveButton("未开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        setAllowBack(true);
                        OpenSettingUtils.openVivoBackground(BackgroundPermissionActivity.this);
                    }
                })
                .create().show();
    }
}
